//
//  scoreLayer.m
//  zhuan
//
//  Created by rexzhao on 11-5-31.
//  Copyright 2011 no. All rights reserved.
//

#import "scoreLayer.h"
#import "HelloWorldLayer.h"
#import "GameLogic.h"
#import "playLayer.h"
#import "SoundManager.h"
#import "GameCenter.h"

//extern int score_;
//extern float add[4];

@implementation scoreLayer

-(CCMenuItem *) CreateMenu:(NSString*)label target:(id)target selector:(SEL)selector {
	CCSprite * button_normal = [CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"dkz_unprass_01.png"]];
	CCSprite * button_select = [CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"dkz_onprass_01.png"]];
	CCMenuItem * button = [CCMenuItemSprite itemFromNormalSprite:button_normal selectedSprite:button_select target:target selector:selector];
	CCLabelTTF * l = [CCLabelTTF labelWithString:label fontName:@"Marker Felt" fontSize:28]; 
	l.color = ccc3(243, 189, 69);
	CGSize bSize = button.contentSize;
	l.position = ccp(bSize.width / 2, bSize.height / 2);
	[button addChild:l];
	return button;
}

-(id) init
{
	// always call "super" init
	// Apple recommends to re-assign "self" with the "super" return value
	if( (self=[super init])) {
        [[GameLogic sharedGameLogic] cleansave];
        
		CGSize size = [[CCDirector sharedDirector] winSize];
		[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"dkz.plist"];

		CCLayer * layer = [CCLayer node];
		CCSprite * sprite = [CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"dkz_jiesuan_bg.png"]];
		[layer addChild:sprite];
		sprite.position = ccp(size.width / 2, size.height / 2);
		
		//score
		NSString * max_score_key = nil;
		if ([GameLogic IsScoreMode]) {
			max_score_key = @"iButtons_1";
		} else if ([GameLogic IsEndlessMode]) {
			max_score_key = @"iButtons_2";
		} else {
			max_score_key = @"iButtons_3";
		}
		NSString *path = [CCFileUtils fullPathFromRelativePath:@"game_config.plist"];
		NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];		
		int max_score = [[dict objectForKey:[NSString stringWithFormat:@"max_score_%@", max_score_key]] intValue];
		if (max_score < score_) {
			max_score = score_;
			NSString *path = [CCFileUtils fullPathFromRelativePath:@"game_config.plist"];
			NSMutableDictionary * dict =[[NSMutableDictionary alloc] initWithContentsOfFile:path];
			[dict setValue:[NSNumber  numberWithInt:max_score] forKey:[NSString stringWithFormat:@"max_score_%@", max_score_key]];
			[dict writeToFile:path atomically:YES];
			[dict release];
			if(game_center_on) {
				[[GameCenter sharedGameCenter] reportScore:score_ forCategory:max_score_key];
			}
		}
		
		CGSize sSize = sprite.contentSize;

		CCLabelTTF * label_levelup = [CCLabelTTF labelWithString:@"Summary" fontName:@"Marker Felt"  fontSize:64];
		label_levelup.position = ccp(sSize.width / 2, sSize.height - 100);
		label_levelup.color = ccc3(64, 0, 0);
		[sprite addChild:label_levelup];
		
		
		CCLabelTTF *label_score_t = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"total score  : %d", score_] fontName:@"Marker Felt" fontSize:32];
		CCLabelTTF *label_score_c = nil;
		if ([GameLogic IsClassicMode]) {
			label_score_c = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"max score    : %d", max_score] fontName:@"Marker Felt" fontSize:32];
		} else {
			label_score_c = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"current score: %d", score_cur_] fontName:@"Marker Felt" fontSize:32];
		}
		
		CCLabelTTF *label_time    = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"time cost  : %d s", (int)time_cost_] fontName:@"Marker Felt" fontSize:32];
		label_score_t.color = label_score_c.color = label_time.color = ccc3(64, 0, 0);
		
		label_score_t.position =  ccp(sSize.width /2 , sSize.height / 2 + 60);
		label_score_c.position =  ccp(sSize.width /2 , sSize.height / 2);
		label_time.position =  ccp(sSize.width /2 , sSize.height/ 2 - 60);
		
		[sprite addChild: label_score_t];
		[sprite addChild: label_score_c];
		[sprite addChild: label_time];
		
//		-(CCMenuItem *) CreateMenu:(NSString*)label target:(id)target selector:(SEL)selector {

		CCMenuItem * nextLevel = [self CreateMenu:@"Retry" target:self selector:@selector(OnRetry:)];
		CCMenuItem * mainMenu = [self CreateMenu:@"Main Menu" target:self selector:@selector(OnMainMenu:)];
		
		nextLevel.position = ccp(size.width / 2 - 120, 80);
		mainMenu.position = ccp(size.width / 2 + 120, 80);
		CCMenu * menu = [CCMenu menuWithItems:nextLevel, mainMenu, nil];
		menu.position = ccp(0,0);
		[layer addChild:menu];
		
		[self addChild:layer];
		//layer.position = ccp(0, -size.height);
		//[layer runAction:[CCMoveTo actionWithDuration:0.5 position:ccp(0, 0)]];
		
		sprite.position = ccp(sprite.position.x - 100, size.height + sSize.height / 2);
		sprite.rotation = -90;
		[sprite runAction:[CCMoveTo actionWithDuration:0.5 position:ccp(size.width / 2, size.height / 2)]];
		[sprite runAction:[CCRotateTo	actionWithDuration:0.5 angle:0]];
		
		NSLog(@"scoreLayer init done\n");
	}
	return self;
}

-(void) OnRetry:(id)sender {
	if (sound_on) [[SoundManager sharedManager] play:@"enter" loops:1];

	[[GameLogic sharedGameLogic] Restart];
	[[CCDirector sharedDirector] replaceScene:
	     [CCTransitionFade transitionWithDuration:1.0f scene:[playLayer scene]]];
}

-(void) OnMainMenu:(id)sender {
	if (sound_on) [[SoundManager sharedManager] play:@"enter" loops:1];

	[[CCDirector sharedDirector] replaceScene:
	 [CCTransitionFade transitionWithDuration:1.0f scene:[HelloWorldLayer scene]]];
}

-(void) dealloc {
	[super dealloc];
}
@end
